package rmiserver;

import handler.CallHandler;

import java.io.IOException;

import util.AppFeatures;
import exception.LipeRMIException;
import net.Server;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 
 * @author salim
 */
public class AppServer extends javax.swing.JFrame {

	private static boolean running = false;
	private static final long serialVersionUID = 1L;
	private final static int DEFAULT_PORT_NUMBER = 1099;
	private static int port = DEFAULT_PORT_NUMBER;

	static AppFeaturesImpl af = null;
	static CallHandler callHandler = new CallHandler();
	static Server server = new Server();

	/**
	 * Creates new form gui
	 */
	public AppServer() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */

	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		portNumberTextField = new javax.swing.JTextField();
		validateButton = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();
		statusLabel = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		portNumberTextField.setText("1099");
		portNumberTextField
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						portNumberTextFieldActionPerformed(evt);
					}
				});

		validateButton.setText("Start Srver");
		validateButton.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				validateButtonActionPerformed(evt);
			}
		});

		jLabel1.setText("Port number");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														statusLabel,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabel1)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		portNumberTextField,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		208,
																		Short.MAX_VALUE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		validateButton,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		116,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														portNumberTextField,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(validateButton)
												.addComponent(jLabel1))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(statusLabel,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										14,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(59, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>

	private void portNumberTextFieldActionPerformed(
			java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void validateButtonActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		port = Integer.valueOf(portNumberTextField.getText());

		if (!running) {
			try {
				af = new AppFeaturesImpl();
				callHandler.registerGlobal(AppFeatures.class, af);
				server.bind(port, callHandler);
				running = true;
				statusLabel.setText("Running on " + port);

			} catch (LipeRMIException | IOException e) {
				// TODO Auto-generated catch block
				statusLabel.setText(e.getMessage());
				e.printStackTrace();
			}

		} else {
			try {
				server.bind(port, callHandler);
				statusLabel.setText("Running on " + port);

			} catch (IOException e) {
				// TODO Auto-generated catch block
				statusLabel.setText(e.getMessage());
				e.printStackTrace();
			}
		}

	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(AppServer.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(AppServer.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(AppServer.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(AppServer.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				new AppServer().setVisible(true);
			}
		});

	}

	// Variables declaration - do not modify
	private javax.swing.JLabel jLabel1;
	private javax.swing.JTextField portNumberTextField;
	private javax.swing.JLabel statusLabel;
	private javax.swing.JButton validateButton;
	// End of variables declaration
}
